package com.ch.clinking.adapter.Order;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.activity.AddOrderActivity;
import com.ch.clinking.activity.EditMerStockActivity;
import com.ch.clinking.activity.EditOrderActivity;
import com.ch.clinking.bean.Merchandise;
import com.ch.clinking.bean.Order;
import com.ch.clinking.bean.OrderDto;

import java.util.List;

public class OrderListAdapter extends ArrayAdapter<OrderDto> {

    private List<OrderDto> ordersList;
    private int resourceId;
    private Context context;
    private FragmentManager fragmentManager;


    boolean color = true;

    private List<String> size;

    public OrderListAdapter(@NonNull Context context, int resource, @NonNull List<OrderDto> orders, FragmentManager fragmentManager) {
        super(context, resource, orders);
        this.context = context;
        this.ordersList = orders;
        this.resourceId = resource;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.merImg = convertView.findViewById(R.id.list_oder_skc_img);
            viewHolder.merTitle = convertView.findViewById(R.id.list_oder_spu_title);
            viewHolder.productNum = convertView.findViewById(R.id.list_order_SKC_product_num);
            viewHolder.state = convertView.findViewById(R.id.list_order_state);
            viewHolder.tableLayout = convertView.findViewById(R.id.stock_order_table);
            viewHolder.listOrderItemContainer = convertView.findViewById(R.id.list_oder_item_container);
//            viewHolder.oderMake = convertView.findViewById(R.id.oder_start);
            viewHolder.oderFinish = convertView.findViewById(R.id.oder_finish);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        OrderDto orderDto = ordersList.get(position);

//        // 设置数据
        viewHolder.merTitle.setText(orderDto.getTitle());
        viewHolder.productNum.setText(orderDto.getProductNumber());
        viewHolder.state.setText(orderDto.getState());

        // 清空表格内容
        viewHolder.tableLayout.removeAllViews();

        // 动态生成表格内容
        getSize(orderDto, viewHolder.tableLayout);

//         加载图片
//        Glide.with(context)
//                .load(OkHttpUtil.getInstance().getIMG_URL() + orderDto.getFirstImage())
//                .into(viewHolder.merImg);


        // 设置默认占位图片
        Glide.with(context)
                .load(R.drawable.appicon)
                .into(viewHolder.merImg);

        // 加载图片
        Glide.with(context)
                .load(OkHttpUtil.getInstance().getIMG_URL() + orderDto.getFirstImage())
                .placeholder(R.drawable.appicon)
                .diskCacheStrategy(DiskCacheStrategy.ALL)  // 缓存策略：缓存原始图片和转换后的图片
                .thumbnail(0.5f)  // 缩略图大小
                .error(R.drawable.appicon)
                .into(viewHolder.merImg);

        // 设置点击事件
        viewHolder.listOrderItemContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, EditOrderActivity.class);
                intent.putExtra("orderDto", orderDto);
                context.startActivity(intent);
            }
        });

        viewHolder.oderMake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(context, AddOrderActivity.class);
//                intent.putExtra("merchandise", merchandise);
//                context.startActivity(intent);
            }
        });

        return convertView;
    }

    // ViewHolder 类
    static class ViewHolder {
        ImageView merImg;
        TextView merTitle;
        TextView productNum;
        TextView state;
        TableLayout tableLayout;
        // 整个布局
        ViewGroup listOrderItemContainer;
        // 下货
        Button oderMake;
        // 出货
        Button oderFinish;
    }

    @SuppressLint("SetTextI18n")
    private void getSize(OrderDto orderDto, TableLayout tableLayout) {
        color = true;
        TableRow sizeRow = new TableRow(context);
        TableRow stockRow = new TableRow(context);
        TableRow makeRow = new TableRow(context);
        sizeRow.setPadding(0, 0, 0, 0);
        stockRow.setPadding(0, 0, 0, 0);
        makeRow.setPadding(0, 0, 0, 0);
        TableRow.LayoutParams editTextParams = new TableRow.LayoutParams(120, TableRow.LayoutParams.MATCH_PARENT);
        editTextParams.gravity = Gravity.CENTER;


        TextView tableTitle = new TextView(context);
        tableTitle.setText("尺码");
        TextView plan = new TextView(context);
        plan.setText("计划");
        TextView tureMake = new TextView(context);
        tureMake.setText("实数");
        setTableLayout(tableTitle, plan, tureMake, sizeRow, stockRow, makeRow, editTextParams);


        if (orderDto.getActivate_XXS()==1) {
            TextView size = new TextView(context);
            size.setText("XXS");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXXS()));
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XXS()));
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }
        if (orderDto.getActivate_XS()==1) {
            TextView size = new TextView(context);
            size.setText("XS");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXS()));
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XXS()));
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }
        if (orderDto.getActivate_S()==1) {
            TextView size = new TextView(context);
            size.setText("S");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getS()));
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_S()));
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }
        if (orderDto.getActivate_M()==1) {
            TextView size = new TextView(context);
            size.setText("M");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getM())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_M())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_L()==1) {
            TextView size = new TextView(context);
            size.setText("L");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_L())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_XL()==1) {
            TextView size = new TextView(context);
            size.setText("XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_XXL()==1) {
            TextView size = new TextView(context);
            size.setText("XXL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXXL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XXL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_0XL()==1) {
            TextView size = new TextView(context);
            size.setText("0XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_0XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_0XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_1XL()==1) {
            TextView size = new TextView(context);
            size.setText("1XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_1XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_1XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_2XL()==1) {
            TextView size = new TextView(context);
            size.setText("2XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_2XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_2XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_3XL()==1) {
            TextView size = new TextView(context);
            size.setText("3XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_3XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_3XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_4XL()==1) {
            TextView size = new TextView(context);
            size.setText("4XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_4XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_4XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_5XL()==1) {
            TextView size = new TextView(context);
            size.setText("5XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_5XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_5XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_6XL()==1) {
            TextView size = new TextView(context);
            size.setText("6XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_6XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_6XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }


        tableLayout.addView(sizeRow);
        tableLayout.addView(stockRow);
        tableLayout.addView(makeRow);
    }

    private void setTableLayout(TextView size, TextView stock, TextView make, TableRow sizeRow, TableRow stockRow, TableRow makeRow, TableRow.LayoutParams editTextParams) {
        size.setLayoutParams(editTextParams);
        stock.setLayoutParams(editTextParams);
        make.setLayoutParams(editTextParams);
        size.setBackgroundColor(getCellColor());
        stock.setBackgroundColor(getCellColor());
        make.setBackgroundColor(getCellColor());
        if (color) {
            color = false;
        } else {
            color = true;
        }
        size.setWidth(120);
        stock.setWidth(120);
        make.setWidth(120);
        size.setPadding(0, 0, 0, 0);
        stock.setPadding(0, 0, 0, 0);
        make.setPadding(0, 0, 0, 0);
        size.setGravity(Gravity.CENTER);
        stock.setGravity(Gravity.CENTER);
        make.setGravity(Gravity.CENTER);
        sizeRow.addView(size);
        stockRow.addView(stock);
        makeRow.addView(make);
    }

    private int getCellColor() {
        if (color) {
            return Color.parseColor("#E7E7E7"); // 偶数索引
        } else {
            return Color.parseColor("#FFFFFF"); // 奇数索引
        }
    }

}
